﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Components;
using Model;


namespace MsDAL
{
   public partial  class Pot3a00Base
    {		
		/// <summary>
        /// 增加一条数据
        /// </summary>
        public virtual  SqlApp Add(Model.Pot3a00 pot3a00)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into Pot3a00(");
			strSql.Append("listid ,productcode ,productname ,barcode ,pvlistid ,ptlistid ,ptype ,status ,ulistid ,brand ,leverid ,price ,bordqty ,taxid ,bprice ,calcode ,pamt ,pct ,storageqty ,wayqty ,nowqty ,condate ,outdate ,muser ,spc ,bpricetax ,pamtax ,tranwayqty )");
			strSql.Append(" values (");
			strSql.Append("@listid ,@productcode ,@productname ,@barcode ,@pvlistid ,@ptlistid ,@ptype ,@status ,@ulistid ,@brand ,@leverid ,@price ,@bordqty ,@taxid ,@bprice ,@calcode ,@pamt ,@pct ,@storageqty ,@wayqty ,@nowqty ,@condate ,@outdate ,@muser ,@spc ,@bpricetax ,@pamtax ,@tranwayqty )");		

			SqlParameter[] parameters = {
				new SqlParameter("@listid",SqlDbType.NVarChar),
				new SqlParameter("@productcode",SqlDbType.NVarChar),
				new SqlParameter("@productname",SqlDbType.NVarChar),
				new SqlParameter("@barcode",SqlDbType.NVarChar),
				new SqlParameter("@pvlistid",SqlDbType.NVarChar),
				new SqlParameter("@ptlistid",SqlDbType.NVarChar),
				new SqlParameter("@ptype",SqlDbType.NVarChar),
				new SqlParameter("@status",SqlDbType.Int),
				new SqlParameter("@ulistid",SqlDbType.NVarChar),
				new SqlParameter("@brand",SqlDbType.NVarChar),
				new SqlParameter("@leverid",SqlDbType.NVarChar),
				new SqlParameter("@price",SqlDbType.Decimal),
				new SqlParameter("@bordqty",SqlDbType.Decimal),
				new SqlParameter("@taxid",SqlDbType.NVarChar),
				new SqlParameter("@bprice",SqlDbType.Decimal),
				new SqlParameter("@calcode",SqlDbType.Int),
				new SqlParameter("@pamt",SqlDbType.Decimal),
				new SqlParameter("@pct",SqlDbType.Decimal),
				new SqlParameter("@storageqty",SqlDbType.Decimal),
				new SqlParameter("@wayqty",SqlDbType.Decimal),
				new SqlParameter("@nowqty",SqlDbType.Decimal),
				new SqlParameter("@condate",SqlDbType.DateTime),
				new SqlParameter("@outdate",SqlDbType.DateTime),
				new SqlParameter("@muser",SqlDbType.NVarChar),
				new SqlParameter("@spc",SqlDbType.NVarChar),
				new SqlParameter("@bpricetax",SqlDbType.Decimal),
				new SqlParameter("@pamtax",SqlDbType.Decimal),
				new SqlParameter("@tranwayqty",SqlDbType.Decimal)
				};
			parameters[0].Value = pot3a00.Listid;
			parameters[1].Value = pot3a00.Productcode;
			parameters[2].Value = pot3a00.Productname;
			parameters[3].Value = pot3a00.Barcode;
			parameters[4].Value = pot3a00.Pvlistid;
			parameters[5].Value = pot3a00.Ptlistid;
			parameters[6].Value = pot3a00.Ptype;
			parameters[7].Value = pot3a00.Status;
			parameters[8].Value = pot3a00.Ulistid;
			parameters[9].Value = pot3a00.Brand;
			parameters[10].Value = pot3a00.Leverid;
			parameters[11].Value = pot3a00.Price;
			parameters[12].Value = pot3a00.Bordqty;
			parameters[13].Value = pot3a00.Taxid;
			parameters[14].Value = pot3a00.Bprice;
			parameters[15].Value = pot3a00.Calcode;
			parameters[16].Value = pot3a00.Pamt;
			parameters[17].Value = pot3a00.Pct;
			parameters[18].Value = pot3a00.Storageqty;
			parameters[19].Value = pot3a00.Wayqty;
			parameters[20].Value = pot3a00.Nowqty;
			parameters[21].Value = pot3a00.Condate;
			parameters[22].Value = pot3a00.Outdate;
			parameters[23].Value = pot3a00.Muser;
			parameters[24].Value = pot3a00.Spc;
			parameters[25].Value = pot3a00.Bpricetax;
			parameters[26].Value = pot3a00.Pamtax;
			parameters[27].Value = pot3a00.Tranwayqty;
			
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");
        }
	
        /// <summary>
        /// 判断某个值是否存在表中的某个字段
        /// </summary>
        /// <param name="Condition">条件字段名 例:A=@A</param>
        /// <param name="hs">参数和值 如hs["@A"]=123</param>
        /// <returns></returns>
        public virtual bool Exists(String condition, Hashtable hs)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) from Pot3a00 ");
			if (!string.IsNullOrEmpty(condition))
				strSql.Append(" where " + condition);
			List<SqlParameter> list = DBHelper.GetListByHashtable(hs);
			if (list.Count > 0)
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString(), list.ToArray())) > 0;
			else
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString())) > 0;
        }

		/// <summary>
        /// 更新一条数据
        /// </summary>
        public virtual SqlApp Update(Model.Pot3a00 pot3a00)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Pot3a00 set ");
			strSql.Append("productcode=@productcode ,productname=@productname ,barcode=@barcode ,pvlistid=@pvlistid ,ptlistid=@ptlistid ,ptype=@ptype ,status=@status ,ulistid=@ulistid ,brand=@brand ,leverid=@leverid ,price=@price ,bordqty=@bordqty ,taxid=@taxid ,bprice=@bprice ,calcode=@calcode ,pamt=@pamt ,pct=@pct ,storageqty=@storageqty ,wayqty=@wayqty ,nowqty=@nowqty ,condate=@condate ,outdate=@outdate ,muser=@muser ,spc=@spc ,bpricetax=@bpricetax ,pamtax=@pamtax ,tranwayqty=@tranwayqty ");
			strSql.Append("where listid=@listid ");
			SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar),
				new SqlParameter("@productcode", SqlDbType.NVarChar),
				new SqlParameter("@productname", SqlDbType.NVarChar),
				new SqlParameter("@barcode", SqlDbType.NVarChar),
				new SqlParameter("@pvlistid", SqlDbType.NVarChar),
				new SqlParameter("@ptlistid", SqlDbType.NVarChar),
				new SqlParameter("@ptype", SqlDbType.NVarChar),
				new SqlParameter("@status", SqlDbType.Int),
				new SqlParameter("@ulistid", SqlDbType.NVarChar),
				new SqlParameter("@brand", SqlDbType.NVarChar),
				new SqlParameter("@leverid", SqlDbType.NVarChar),
				new SqlParameter("@price", SqlDbType.Decimal),
				new SqlParameter("@bordqty", SqlDbType.Decimal),
				new SqlParameter("@taxid", SqlDbType.NVarChar),
				new SqlParameter("@bprice", SqlDbType.Decimal),
				new SqlParameter("@calcode", SqlDbType.Int),
				new SqlParameter("@pamt", SqlDbType.Decimal),
				new SqlParameter("@pct", SqlDbType.Decimal),
				new SqlParameter("@storageqty", SqlDbType.Decimal),
				new SqlParameter("@wayqty", SqlDbType.Decimal),
				new SqlParameter("@nowqty", SqlDbType.Decimal),
				new SqlParameter("@condate", SqlDbType.DateTime),
				new SqlParameter("@outdate", SqlDbType.DateTime),
				new SqlParameter("@muser", SqlDbType.NVarChar),
				new SqlParameter("@spc", SqlDbType.NVarChar),
				new SqlParameter("@bpricetax", SqlDbType.Decimal),
				new SqlParameter("@pamtax", SqlDbType.Decimal),
				new SqlParameter("@tranwayqty", SqlDbType.Decimal)
				};
			parameters[0].Value = pot3a00.Listid;
			parameters[1].Value = pot3a00.Productcode;
			parameters[2].Value = pot3a00.Productname;
			parameters[3].Value = pot3a00.Barcode;
			parameters[4].Value = pot3a00.Pvlistid;
			parameters[5].Value = pot3a00.Ptlistid;
			parameters[6].Value = pot3a00.Ptype;
			parameters[7].Value = pot3a00.Status;
			parameters[8].Value = pot3a00.Ulistid;
			parameters[9].Value = pot3a00.Brand;
			parameters[10].Value = pot3a00.Leverid;
			parameters[11].Value = pot3a00.Price;
			parameters[12].Value = pot3a00.Bordqty;
			parameters[13].Value = pot3a00.Taxid;
			parameters[14].Value = pot3a00.Bprice;
			parameters[15].Value = pot3a00.Calcode;
			parameters[16].Value = pot3a00.Pamt;
			parameters[17].Value = pot3a00.Pct;
			parameters[18].Value = pot3a00.Storageqty;
			parameters[19].Value = pot3a00.Wayqty;
			parameters[20].Value = pot3a00.Nowqty;
			parameters[21].Value = pot3a00.Condate;
			parameters[22].Value = pot3a00.Outdate;
			parameters[23].Value = pot3a00.Muser;
			parameters[24].Value = pot3a00.Spc;
			parameters[25].Value = pot3a00.Bpricetax;
			parameters[26].Value = pot3a00.Pamtax;
			parameters[27].Value = pot3a00.Tranwayqty;
	
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");			
		}
		
		public virtual SqlApp Delete(Model.Pot3a00 pot3a00)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("DELETE FROM  Pot3a00  ");
			strSql.Append("WHERE listid=@listid ");
			SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
			parameters[0].Value = pot3a00.Listid;
	
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");		
		}
		
		/// <summary>
        /// 根据Listid得到整个 Pot3a00对象,如果没查到，返回null
        /// </summary>
        /// <param name="Listid">该实体主键 Listid</param>
        /// <returns>实体Pot3a00的对象</returns>
		public virtual Model.Pot3a00 GetModelById(string Listid)
        {
			string where = "listid=@listid ";
			
			string sql = GetSql(where,null);
			
			List<SqlParameter> parameters = new List<SqlParameter>();
			
			SqlParameter p0 = new SqlParameter("@Listid",SqlDbType.NVarChar);
			p0.Value = Listid;
			parameters.Add(p0);
			
			DataTable dt = DBHelper.GetData(sql, parameters.ToArray());
			
			//调用实体Pot3a00的GetPot3a00ListByDataTable方法得到实体对象
			List<Model.Pot3a00> list =Model.Pot3a00.GetPot3a00ListByDataTable(dt);
			
			return  (list.Count>0) ? list[0] : null;
        }
		
		
		 /// <summary>
        /// 得到Sql语句
        /// </summary>
        /// <param name="Condition">Sql语句的条件</param>
		/// <param name="OrderBy">排序条件，默认为String.Empty</param>
        /// <returns></returns>
        protected virtual String GetSql(string condition,string orderBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [listid] ,[productcode] ,[productname] ,[barcode] ,[pvlistid] ,[ptlistid] ,[ptype] ,[status] ,[ulistid] ,[brand] ,[leverid] ,[price] ,[bordqty] ,[taxid] ,[bprice] ,[calcode] ,[pamt] ,[pct] ,[storageqty] ,[wayqty] ,[nowqty] ,[condate] ,[outdate] ,[muser] ,[spc] ,[bpricetax] ,[pamtax] ,[tranwayqty] ");
            strSql.Append(" FROM Pot3a00 ");
 			
			if (!string.IsNullOrEmpty(condition)) strSql.Append(" where " + condition);				
			if(!string.IsNullOrEmpty(orderBy)) strSql.Append( " order by " + orderBy );
			
            return  strSql.ToString();
        }

        /// <summary>
        /// 根据条件和参数得到Pot3a00对象
        /// </summary>
        /// <param name="Condition">Sql条件</param>
        /// <param name="hs">参数和值</param>
        /// <returns></returns>
        public virtual Model.Pot3a00 GetModel(String Condition, Hashtable hs)
        {
			String sql = GetSql(Condition,String.Empty);
			DataTable dt = DBHelper.GetTableByCondition(sql,  hs);
			return (dt.Rows.Count > 0) ? Model.Pot3a00.GetPot3a00ListByDataTable(dt)[0] : null;
        }

        /// <summary>
        /// 根据主键的值和某字段得到其值,可以用来判断是否存在
        /// </summary>
        /// <param name="TableKeyValue">主键值</param>
        /// <param name="FieldName">要查询的字段名称</param>
        /// <returns></returns>
        public virtual Object GetValueByField(Object TableKeyValue, Object FieldName)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select " + FieldName + " from Pot3a00 ");
			strSql.Append(" where listid=@TableKeyValue ");
			SqlParameter p = new SqlParameter("@TableKeyValue", TableKeyValue);

			return DBHelper.ExecuteScalar(strSql.ToString(), p);
        }
		
        /// <summary>
        /// 分页SQL
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual ReturnPageData GetList(Pager page)
        {
            ReturnPageData data = new ReturnPageData();
            //page.PageIndex++;
            page.PrimaryKey = "listid ";

            page.ShowFields = " [listid] ,[productcode] ,[productname] ,[barcode] ,[pvlistid] ,[ptlistid] ,[ptype] ,[status] ,[ulistid] ,[brand] ,[leverid] ,[price] ,[bordqty] ,[taxid] ,[bprice] ,[calcode] ,[pamt] ,[pct] ,[storageqty] ,[wayqty] ,[nowqty] ,[condate] ,[outdate] ,[muser] ,[spc] ,[bpricetax] ,[pamtax] ,[tranwayqty] ";

            page.TableName = " Pot3a00 ";

			page.StrWhere = string.IsNullOrEmpty(page.StrWhere) ? " 1=1 " : page.StrWhere;
			
            page.StrWhere = string.IsNullOrEmpty(page.AutoWhere) ? page.StrWhere : page.StrWhere + " And ("+ page.AutoWhere +")";

            page.For = PagerFor.Count;
            DataTable dtRowCount = DBHelper.GetSelectLimit(page);
            if (dtRowCount == null || dtRowCount.Rows.Count == 0)
            {
                data.AllRowCount = 0;
                return data; //NULL DATA
            }
            data.AllRowCount = Convert.ToInt32(dtRowCount.Rows[0]["Total"]);
            page.For = PagerFor.Data;

            DataTable dtPageData = DBHelper.GetSelectLimit(page);
            data.CurrnetPageData = dtPageData;

            //汇总
            page.For = PagerFor.Summary;
            data.SummaryData = DBHelper.GetSelectLimit(page);
            
            return data;
        }		
		
		public virtual DataTable GetList(string c,Hashtable hs)
		{
			string sql = GetSql(c,null); //得到SQL语句
			//返回结果集
			return DBHelper.GetData(sql,DBHelper.GetListByHashtable(hs).ToArray());
		}
    }
}
